<?php
namespace app\mxty\model;

use think\Model;
use think\model\concern\SoftDelete;
use app\common\model\UploadFile;
use app\common\enum\order\PayStatus as OrderPayStatusEnum;

class MemberCardOrder extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $table = 'athlon_membercard_order';
    protected $pk = 'order_id';

    public function image()
    {
        return $this->hasOne(UploadFile::class, 'file_id', 'image_id');
    }

    public static function order($data)
    {
        return self::create($data);
    }

    public static function getInfoByOderNo($order_no)
    {
        return self::where('order_no', $order_no)->find();
    }

    // 根据订单id和用户id查找订单
    public function getInfoById($order_id, $user_id)
    {
        return $this->with('image')->where(['order_id' => $order_id, 'user_id' => $user_id, 'pay_status' => OrderPayStatusEnum::SUCCESS])->findOrEmpty();
    }

    public function getCardsByUserId($user_id)
    {
        return self::with(['image'])->where('user_id', $user_id)->where('pay_status', OrderPayStatusEnum::SUCCESS)->select();
    }

    

    // 使用一次有限次数卡
    public function useOneTimes()
    {
        $this->used_times += 1;
        $this->save();
    }
    
}
